/*
 *  gtk_icon.c
 *
 *  GTK+ icon preloading code.
 *
 * Copyright (c) 2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, is prohibited except by
 * Karlan Thomas Mitchell.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "gtk_icon.h"


GdkPixbuf * filename_icon ( struct st_iconset * iconset, char * filename){
	   char mime_icon[20];
	   char * extension = strrchr(filename, '.');
            if(extension != NULL){
                 
                 
                 if(strcasecmp(extension, ".mp3") == 0)
                      strcpy(mime_icon, "audio");
                 else  if(strcasecmp(extension, ".mp4") == 0)
                      strcpy(mime_icon, "audio");
                 else  if(strcasecmp(extension, ".m4a") == 0)
                      strcpy(mime_icon, "audio");
                      
                      
                 else  if(strcasecmp(extension, ".mpg") == 0)
                      strcpy(mime_icon, "video");
                 else  if(strcasecmp(extension, ".mpeg") == 0)
                      strcpy(mime_icon, "video");
                      
                      
                 else  if(strcasecmp(extension, ".png") == 0)
                      strcpy(mime_icon, "image");
                 else  if(strcasecmp(extension, ".gif") == 0)
                      strcpy(mime_icon, "image");
                 else  if(strcasecmp(extension, ".svg") == 0)
                      strcpy(mime_icon, "image");
                 else  if(strcasecmp(extension, ".jpg") == 0)
                      strcpy(mime_icon, "image");
                 else  if(strcasecmp(extension, ".jpeg") == 0)
                      strcpy(mime_icon, "image");
                 else  if(strcasecmp(extension, ".tiff") == 0)
                      strcpy(mime_icon, "image");
                 else  if(strcasecmp(extension, ".xpm") == 0)
                      strcpy(mime_icon, "image");
                      
                      
                 else  if(strcasecmp(extension, ".msi") == 0)
                      strcpy(mime_icon, "exe");
                 else  if(strcasecmp(extension, ".exe") == 0)
                      strcpy(mime_icon, "exe");
                      
                 else  if(strcasecmp(extension, ".zip") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".gz") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".bz2") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".tar") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".tar.bz2") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".tar.gz") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".rar") == 0)
                      strcpy(mime_icon, "package");
                 else  if(strcasecmp(extension, ".7z") == 0)
                      strcpy(mime_icon, "package");
                      
                      
                      
                 else  if(strcasecmp(extension, ".bat") == 0)
                      strcpy(mime_icon, "script");
                 else  if(strcasecmp(extension, ".sh") == 0)
                      strcpy(mime_icon, "script");
                 else  if(strcasecmp(extension, ".py") == 0)
                      strcpy(mime_icon, "script");
                      
                      
                      
                 else  if(strcasecmp(extension, ".html") == 0)
                      strcpy(mime_icon, "html");
                 else  if(strcasecmp(extension, ".htm") == 0)
                      strcpy(mime_icon, "html");
                      
                 else  if(strcasecmp(extension, ".c") == 0)
                      strcpy(mime_icon, "cfile");
                 else  if(strcasecmp(extension, ".cpp") == 0)
                      strcpy(mime_icon, "cfile");
                 else  if(strcasecmp(extension, ".h") == 0)
                      strcpy(mime_icon, "cfile");
                 else  if(strcasecmp(extension, ".hpp") == 0)
                      strcpy(mime_icon, "cfile");
                      
                      
                      
                 else  if(strcasecmp(extension, ".txt") == 0)
                      strcpy(mime_icon, "txt");
                      
                      
                      
                 else  if(strcasecmp(extension, ".doc") == 0)
                      strcpy(mime_icon, "doc");
                 else  if(strcasecmp(extension, ".rtf") == 0)
                      strcpy(mime_icon, "doc");
                 else  if(strcasecmp(extension, ".ps") == 0)
                      strcpy(mime_icon, "doc");
                 else  if(strcasecmp(extension, ".pdf") == 0)
                      strcpy(mime_icon, "doc");
                 else  if(strcasecmp(extension, ".sxw") == 0)
                      strcpy(mime_icon, "doc");
                 else  if(strcasecmp(extension, ".odt") == 0)
                      strcpy(mime_icon, "doc");
                      
                      
                      
                 else
                      strcpy(mime_icon, "generic");
                 }
    	else
            strcpy(mime_icon, "generic");

    	return iconset_get(iconset, mime_icon);
	}

GdkPixbuf * iconset_get ( struct st_iconset * iconset, char * icon_name){
          return (GdkPixbuf *)mdb_get_address(iconset, icon_name);
          }
          
          
          
int  iconset_set ( struct st_iconset * iconset, char * icon_name, char * directory){
          GError    * error    = NULL;
          char icon_buf[PATH_MAX];
	  #ifdef WIN32
          snprintf(icon_buf, PATH_MAX, "%s\\%s.xpm", directory, icon_name);
	  #else
          snprintf(icon_buf, PATH_MAX, "%s/%s.xpm", directory, icon_name);
          #endif 
	 GdkPixbuf * new_icon = gdk_pixbuf_new_from_file(icon_buf, &error);


          if (error){
             g_warning ("Could not load icon: %s\n", error->message);
             g_error_free(error);
             return -1;
             }
          mdb_set_address(iconset, icon_name, new_icon);
          return 0;
          }
